package com.wbao.dianniu.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wbao.dianniu.R;
import com.wbao.dianniu.customView.CanDoBlankGridView;
import com.wbao.dianniu.customView.SpanTextView;
import com.wbao.dianniu.data.QuestAnswerListData;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.AnswerPraiseManager;
import com.wbao.dianniu.utils.AnswerListType;
import com.wbao.dianniu.utils.CommentLongClickHelper;
import com.wbao.dianniu.utils.PictureDownloadUtils;
import com.wbao.dianniu.utils.ReportType;
import com.wbao.dianniu.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 心情问答详情
 *
 * @author yejianjun
 */
public class AmAnswerAdapter extends BaseAdapter {

    private Context mContext;
    private List<QuestAnswerListData> mList = new ArrayList<QuestAnswerListData>();

    public AmAnswerAdapter(Context context) {
        mContext = context;
    }

    public void addData(List<QuestAnswerListData> list) {
        for (int i = 0; i < list.size(); i++) {
            mList.add(list.get(i));
        }
    }

    public List<QuestAnswerListData> getList() {
        return mList;
    }

    public void addOneData(QuestAnswerListData data) {
        if (null != mList) {
            mList.add(0, data);
        }
    }

    public void deleteOneData(int position) {
        mList.remove(position);
        notifyDataSetChanged();
    }

    public void deleteById(int id) {
        if (null == mList)
            return;
        for (int i = 0; i < mList.size(); i++) {
            if (id == mList.get(i).id) {
                mList.remove(i);
                break;
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (null == mList)
            return 0;
        else
            return mList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        if (null == convertView) {
            LayoutInflater mInflater = LayoutInflater.from(mContext);
            convertView = mInflater.inflate(R.layout.quest_answer_list_item, null);
            holder = new Holder();
            holder.headIV = convertView.findViewById(R.id.user_head);
            holder.nameTV = convertView.findViewById(R.id.user_name);
            holder.sexIV = convertView.findViewById(R.id.user_sex);
            holder.partnerIV = convertView.findViewById(R.id.user_partner);
            holder.labelRelative = convertView.findViewById(R.id.user_small_relative);
            holder.labelTV = convertView.findViewById(R.id.user_label);
            holder.addV = convertView.findViewById(R.id.add_v_iv);

            holder.goodCounts = convertView.findViewById(R.id.quest_answer_praise_tv);
            holder.sandianIV = convertView.findViewById(R.id.quest_answer_comment_sandian);
            holder.content = convertView.findViewById(R.id.quest_answer_content);
            holder.gridView = convertView.findViewById(R.id.noScroollGridView);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        if (null == mList || mList.size() < position) {
            return convertView;
        }
        QuestAnswerListData data = mList.get(position);
        if (null != data) {
            PictureDownloadUtils.displayHeadImage(mContext, holder.headIV, data.userInfo.headPic);
            holder.nameTV.setText(data.aliasName == null ? "" : data.aliasName);
            holder.labelRelative.setVisibility(View.GONE);
            holder.sexIV.setVisibility(View.GONE);
            Utils.praiseChanged(mContext, data.isGood, data.goodCount, holder.goodCounts);

            holder.content.setEmojiText(data.content,data.noticeJson);
//            Utils.showSpanContentComment(mContext, data.content, data.noticeJson, holder.content, false);
//			holder.content.setMovementMethod(LinkMovementMethod.getInstance());
//			holder.content.setAutoLinkMask(Linkify.WEB_URLS);
//			holder.content.setText(data.content == null ? "" : data.content);
        }
        //点击事件
        OnClickListener click = new clickListener(data);
        holder.content.setOnClickListener(click);
        holder.goodCounts.setOnClickListener(new praiseListener(position, data));
        holder.sandianIV.setOnClickListener(click);
//        holder.content.setOnLongClickListener(new longClickListener(data));
        return convertView;
    }


    /*class longClickListener implements View.OnLongClickListener {
        private QuestAnswerListData data;

        public longClickListener(QuestAnswerListData data) {
            this.data = data;
        }

        @Override
        public boolean onLongClick(View v) {

            CommentLongClickHelper.getInstance().commentLongClick(mContext, data, ReportType.TYPE_REPLY);
            return false;
        }
    }*/

    /**
     * 点击@功能
     */
    class clickListener implements OnClickListener {
        private QuestAnswerListData data;

        public clickListener(QuestAnswerListData data) {
            this.data = data;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.quest_answer_content:
                    CommentLongClickHelper.getInstance().commentLongClick(mContext, data, ReportType.TYPE_REPLY, data.aliasName, 1, AnswerListType.TYPE_AM);
                    /*if (accountId != GlobalContext.getAccountId()) {
                        ReplyHelper.getInstance().reply(aliasName, accountId);
                        // 弹出软键盘
                    }*/
                    break;
                default:
                    break;
            }

        }

    }

    /**
     * 问题回答点赞
     */
    class praiseListener implements OnClickListener {
        private int position;
        private QuestAnswerListData data;

        public praiseListener(int position, QuestAnswerListData data) {
            this.position = position;
            this.data = data;
        }

        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.quest_answer_praise_tv) {
                if (0 == data.isGood) {
                    data.isGood = 1;
                    data.goodCount++;
                } else {
                    data.isGood = 0;
                    data.goodCount--;
                }
                notifyUI(position, data);
                //通知后台更新数据
                AnswerPraiseManager manager = new AnswerPraiseManager(mContext);
                manager.answerPraise(GlobalContext.getAccountId(), data.id);
            }

        }

    }

    private void notifyUI(int position, QuestAnswerListData data) {
        if (null != data && null != mList) {
            mList.set(position, data);
        }
        notifyDataSetChanged();
    }

    private class Holder extends UserInfoHolder {
//        public ImageView sexIV;
//        public ImageView head;
//        public RelativeLayout labelRelative;
//        public TextView name;
        public ImageView sandianIV;
        public TextView goodCounts;
        public SpanTextView content;
        public CanDoBlankGridView gridView;
    }

}
